package com.maple.p2pclient.http;

import com.maple.p2pclient.utils.SSLSocketClient;
import com.maple.p2pclient.utils.TrustAllManager;

import java.security.SecureRandom;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import okhttp3.OkHttpClient;

public class RetrofitClient extends BaseRetrofitClient {


    private  ApiService apiService;

    public ApiService getApiService(String baseUrl) {
        if (apiService == null) {
            apiService = getApiService(ApiService.class,baseUrl);
        }
        return apiService;
    }



    @Override
    protected OkHttpClient.Builder generateOkHttpBuilder(OkHttpClient.Builder builder) {
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{new TrustAllManager()}, new SecureRandom());
            SSLSocketFactory sslSocketFactory = SSLSocketClient.createSSLSocketFactory();
            HostnameVerifier hostnameVerifier = (hostname, session) -> true;
            return builder
                    .sslSocketFactory(sslSocketFactory, new TrustAllManager())
                    .hostnameVerifier(hostnameVerifier);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
